package com.zhoug.zmui3.chart;

import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;

import com.zhoug.common3.utils.LogUtils;
import com.zhoug.common3.utils.MathUtils;
import com.zhoug.zmui3.R;

import java.util.Locale;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

/**
 * 仪表盘 图表
 * zmui_style_ZMUIDashboardView1
 *
 * @Author 35574
 * @Date 2021/1/22
 * @Description
 */
public class ZMUIDashboardView1 extends View {
    private static final String TAG = ">>>ZMUIDashboardView";
    private static final boolean DEBUG = false;

    private final int DEF_SIZE;//默认宽高大小
    private final float oneDp;
    private static final int DEF_COLOR = Color.BLACK;
    public static final int LABEL_POSITION_INNER = 0;
    public static final int LABEL_POSITION_OUTER = 1;

    /**
     * 刻度文本的位置:弧形外面,弧形里面
     * {@link #LABEL_POSITION_INNER}
     * {@link #LABEL_POSITION_OUTER}
     */
    private int labelPosition;
    /**
     * 刻度文本的颜色
     */
    private int labelColor;
    /**
     * 刻度文本的大小
     */
    private float labelSize;
    /**
     * 标签和弧形的距离
     */
    private float labelMargin;
    /**
     * 弧形度数(包括偏移度数):例:弧度180,两头偏移1.设置成182
     */
    private int arcDegrees;
    /**
     * 两边偏移的度数:弧形开始和结尾偏移的度数:0,两边刻度和弧线对齐,>0:两边弧线超出刻度
     */
    private float arcOffsetDegrees;//开始和结尾偏移的度数

    /**
     * 弧的颜色
     */
    private int arcColor;
    /**
     * 弧的宽度
     */
    private float arcStrokeWidth;

    /**
     * 指针图片
     */
    private Drawable pointerDrawable;
    /**
     * 指针长度和圆弧半径长度的比
     */
    private float pointerDrawableLengthRatio;
    /**
     * 指针图标旋转中心点到图片边界的长度和图片长度的比例
     */
    private float pointerDrawableCenterRatio;

    /**
     * 最小值
     */
    private float minValue;
    /**
     * 最大值
     */
    private float maxValue;
    /**
     * 当前值
     */
    private float value;

    /**
     * 上次的值用于动画
     */
    private float oldValue;
    /**
     * 刻度数目:这里表示把弧度等分的份数,实际刻度数目要多1个
     */
    private int clockPointCount;
    /**
     * 每几个刻度画一个ValueLabel,0不画标签
     */
    private int drawLabelInterval;
    /**
     * 刻度的长度
     */
    private float clockPointLength;
    /**
     * 刻度的宽度
     */
    private float clockPointWidth;
    /**
     * 刻度的颜色
     */
    private int clockPointColor;
    /**
     * 画标签时是否画成长刻度
     */
    private boolean drawLabelIntervalLong;


    private float mRadius;//半径
    private float mCenterX;
    private float mCenterY;

    private Paint mPaint;
    private TextPaint mTextPaint;
    private ValueFormat valueFormat;
    private RectF arcRectF = new RectF();
    private Paint.FontMetrics mFontMetrics = new Paint.FontMetrics();
    private static final float D=20;

    public ZMUIDashboardView1(Context context) {
        this(context, null);
    }

    public ZMUIDashboardView1(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, R.attr.zmui_style_ZMUIDashboardView1);
    }

    public ZMUIDashboardView1(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //默认150dp
        oneDp = context.getResources().getDisplayMetrics().density;
        if (DEBUG) {
            LogUtils.d(TAG, "ZMUIDashboardView1:oneDp=" + oneDp);
        }
        DEF_SIZE = (int) (oneDp * 150 + 0.5);

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ZMUIDashboardView1, defStyleAttr, R.style.zmui_style_ZMUIDashboardView1);
        labelPosition = a.getInt(R.styleable.ZMUIDashboardView1_zmui_dashboard_labelPosition, LABEL_POSITION_INNER);
        labelColor = a.getColor(R.styleable.ZMUIDashboardView1_zmui_dashboard_labelColor, DEF_COLOR);

        labelSize = a.getDimension(R.styleable.ZMUIDashboardView1_zmui_dashboard_labelSize, 10 * oneDp);
        labelMargin = a.getDimension(R.styleable.ZMUIDashboardView1_zmui_dashboard_labelMargin, 5 * oneDp);
        arcDegrees = a.getInt(R.styleable.ZMUIDashboardView1_zmui_dashboard_arcDegrees, 182);
        arcOffsetDegrees = a.getFloat(R.styleable.ZMUIDashboardView1_zmui_dashboard_arcOffsetDegrees, 1f);
        arcStrokeWidth = a.getDimension(R.styleable.ZMUIDashboardView1_zmui_dashboard_arcStrokeWidth, 5 * oneDp);
        arcColor = a.getColor(R.styleable.ZMUIDashboardView1_zmui_dashboard_arcColor, DEF_COLOR);
        minValue = a.getFloat(R.styleable.ZMUIDashboardView1_zmui_dashboard_minValue, 0);
        maxValue = a.getFloat(R.styleable.ZMUIDashboardView1_zmui_dashboard_maxValue, 100);
        value = a.getFloat(R.styleable.ZMUIDashboardView1_zmui_dashboard_value, 0);
        clockPointCount = a.getInt(R.styleable.ZMUIDashboardView1_zmui_dashboard_clockPointCount, 30);
        drawLabelInterval = a.getInt(R.styleable.ZMUIDashboardView1_zmui_dashboard_drawLabelInterval, 3);
        drawLabelIntervalLong = a.getBoolean(R.styleable.ZMUIDashboardView1_zmui_dashboard_drawLabelIntervalLong, true);
        clockPointLength = a.getDimension(R.styleable.ZMUIDashboardView1_zmui_dashboard_clockPointLength, 5 * oneDp);
        clockPointWidth = a.getDimension(R.styleable.ZMUIDashboardView1_zmui_dashboard_clockPointWidth, 2 * oneDp);
        clockPointColor = a.getColor(R.styleable.ZMUIDashboardView1_zmui_dashboard_clockPointColor, DEF_COLOR);
        pointerDrawable = a.getDrawable(R.styleable.ZMUIDashboardView1_zmui_dashboard_pointerDrawable);
        pointerDrawableCenterRatio = a.getFloat(R.styleable.ZMUIDashboardView1_zmui_dashboard_pointerDrawableCenterRatio, 0);
        pointerDrawableLengthRatio = a.getFloat(R.styleable.ZMUIDashboardView1_zmui_dashboard_pointerDrawableLengthRatio, 0.7f);
        //默认指针
        if (pointerDrawable == null) {
            pointerDrawable = ContextCompat.getDrawable(context, R.drawable.zmui_pointer);
            pointerDrawableCenterRatio = 0.0914f;
        }
        a.recycle();

        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setAntiAlias(true);

        mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
        mTextPaint.setAntiAlias(true);

//        setLayerType(LAYER_TYPE_HARDWARE,null);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int measureWidth = 0;
        int measureHeight = 0;
        if (DEBUG) {
            LogUtils.d(TAG, "onMeasure:widthSize=" + widthSize + ",heightSize=" + heightSize);
        }
        if (widthMode == MeasureSpec.EXACTLY && heightMode == MeasureSpec.EXACTLY) {
            //宽高确定
            if (DEBUG) {
                LogUtils.d(TAG, "onMeasure:宽高确定");
            }
            measureWidth = widthSize;
            measureHeight = heightSize;
        } else if (widthMode == MeasureSpec.EXACTLY) {
            //宽度确定,高度自适应
            if (DEBUG) {
                LogUtils.d(TAG, "onMeasure:宽度确定,高度自适应");
            }
            measureWidth = widthSize;
            //用宽度计算内容区域
            int d = measureWidth - getPaddingStart() - getPaddingEnd();
            //计算出的最大高度
            measureHeight = getPaddingTop() + getPaddingBottom() + d;
            if (heightSize > 0) {
                measureHeight = Math.min(heightSize, measureHeight);
            } else {
                measureHeight = Math.min(DEF_SIZE, measureHeight);
            }
        } else if (heightMode == MeasureSpec.EXACTLY) {
            //高度确定,宽度自适应
            if (DEBUG) {
                LogUtils.d(TAG, "onMeasure:高度确定,宽度自适应");
            }
            measureHeight = heightSize;
            int d = measureHeight - getPaddingTop() - getPaddingBottom();
            measureWidth = getPaddingStart() + getPaddingEnd() + d;
            if (widthSize > 0) {
                measureWidth = Math.min(widthSize, measureWidth);
            } else {
                measureWidth = Math.min(DEF_SIZE, measureWidth);
            }
        } else {
            if (DEBUG) {
                LogUtils.d(TAG, "onMeasure:宽高自适应");
            }
            if (widthSize > 0) {
                measureWidth = Math.min(DEF_SIZE, widthSize);
            } else {
                measureWidth = DEF_SIZE;
            }
            if (heightSize > 0) {
                measureHeight = Math.min(DEF_SIZE, heightSize);
            } else {
                measureHeight = DEF_SIZE;
            }

            int size = Math.min(measureWidth - getPaddingStart() - getPaddingEnd(), measureHeight - getPaddingTop() - getPaddingBottom());
            measureWidth = size;
            measureHeight = size;
        }
        if (DEBUG) {
            LogUtils.d(TAG, "onMeasure:measureWidth=" + measureWidth + ",measureHeight=" + measureHeight);
        }
        float textW=0;
        float textH=0;
        if(labelPosition==LABEL_POSITION_OUTER){
            mTextPaint.getFontMetrics(mFontMetrics);
             textW = measureClockText();
             textH = mFontMetrics.bottom-mFontMetrics.top;
        }



        //宽度限制的最大半径
        int maxRadius = (int) ((measureWidth - textW * 2 - labelMargin * 2 - getPaddingStart() - getPaddingEnd()) / 2);
        if (arcDegrees < 180) {
            //高度限制的最大半径
            mRadius = measureHeight - (getPaddingTop() + getPaddingBottom() + textH * 2 + labelMargin * 2);
        } else {
            //高度限制的最大半径
            float startDegree = getStartDegree();
            mRadius = (float) ((measureHeight - (getPaddingTop() + getPaddingBottom() + textH * 2 + labelMargin * 2)) / (1 + Math.abs(Math.sin(startDegree * Math.PI / 180))));
        }
        //取最小值为真实半径
        mRadius = Math.min(maxRadius, mRadius);
        //根据半径重新计算出高度
        float  contentHeight = (measureArcHeight(mRadius, arcDegrees) + getPaddingTop() + getPaddingBottom()) + textH * 2 + labelMargin * 2;
        if(heightMode!=MeasureSpec.EXACTLY){
            measureHeight= (int) (contentHeight+0.5f);
        }
        //画弧度的圆心
        mCenterX = getPaddingStart() + ((measureWidth - getPaddingStart() - getPaddingEnd()) >> 1);
        mCenterY = getPaddingTop() + textH + labelMargin + mRadius+ ((measureHeight - (contentHeight)) / 2);
        if (DEBUG) {
            LogUtils.d(TAG, String.format(Locale.CANADA, "mRadius=%s,mCenterX=%s,mCenterY=%s", mRadius, mCenterX, mCenterY));
            if (DEBUG) {
                LogUtils.d(TAG, "onMeasure:measureWidth=" + measureWidth + ",measureHeight=" + measureHeight);
            }
        }

        setMeasuredDimension(measureWidth, measureHeight);
    }

    /**
     * 计算弧度的高度
     *
     * @param radius
     * @param degree
     * @return
     */
    private float measureArcHeight(float radius, float degree) {
        if (degree == 0) {
            return 0;
        }
        degree = Math.abs(degree % 360);
        if (degree == 0) {
            return radius * 2;
        } else if (degree <= 180) {
            return radius;
        } else {
            float arcYPoint = MathUtils.getArcYPoint(0, radius, getStartDegree());
            return radius + Math.abs(arcYPoint);
        }
    }

    /**
     * 弧度的开始角度
     *
     * @return
     */
    private float getStartDegree() {
        return 180 + ((180 - arcDegrees) >> 1);
    }

    /**
     * 测量文本尺寸:宽
     */
    private float measureClockText() {
        mTextPaint.setTextSize(labelSize);
        ValueFormat valueFormat = getValueFormat();
        String text = valueFormat.getValueFormat(maxValue);
        float w = mTextPaint.measureText(text);
        if (DEBUG) {
            LogUtils.d(TAG, "measureClockText:" + w);
        }
        return w;
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawArc(canvas);
        drawDividerAndText(canvas);
        drawPointer(canvas);
    }




    /**
     * 画弧
     *
     * @param canvas
     */
    private void drawArc(Canvas canvas) {
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(arcStrokeWidth);
        float ban = arcStrokeWidth / 2;//弧线宽度的一半
        mPaint.setColor(arcColor);
        arcRectF.left = mCenterX - mRadius + ban;
        arcRectF.top = mCenterY - mRadius + ban;
        arcRectF.right = mCenterX + mRadius - ban;
        arcRectF.bottom = mCenterY + mRadius - ban;

        float startDegree = getStartDegree();
        canvas.drawArc(arcRectF, startDegree, arcDegrees, false, mPaint);

    }

    /**
     * 绘制刻度和标签
     * 旋转绘制发
     *
     * @param canvas
     */
    private void drawDividerAndText(Canvas canvas) {
        float startDegree = getStartDegree() + arcOffsetDegrees;
        //每份的弧度
        float pDegree = getPDegrees();

        mTextPaint.setColor(labelColor);
        mTextPaint.setTextSize(labelSize);
        mPaint.setColor(clockPointColor);
        mPaint.setStrokeWidth(clockPointWidth);
        int cPosition = (clockPointCount) / 2;//圆弧上中点的位子
        //是否画标签
        boolean drawLabel = false;
        float startX = mCenterX - mRadius + arcStrokeWidth;
        float endXShort = startX + clockPointLength;//短刻度
        float endXLong = startX + clockPointLength * 2;//长刻度
        float yIndexStart = mCenterY - clockPointWidth / 2;//第一个刻度的y坐标单独列出来否则会超出圆弧
        float yIndexEnd = mCenterY + clockPointWidth / 2;//最后一个刻度的y坐标单独列出来否则会超出圆弧
        float yOther = mCenterY;//其它刻度的y坐标,指针对齐刻度中间
        float y;
        float endX;
        canvas.save();
        float startRotate = startDegree - 180;
        canvas.rotate(startRotate, mCenterX, mCenterY);
        //画刻度
        for (int i = 0; i <= clockPointCount; i++) {
            if (drawLabelInterval != 0) {
                drawLabel = i % drawLabelInterval == 0;
            }
            //长刻度
            if (drawLabel && drawLabelIntervalLong) {
                endX = endXLong;
            } else {
                endX = endXShort;
            }
            if (i == 0) {
                y = yIndexStart;
            } else if (i == clockPointCount) {
                y = yIndexEnd;
            } else {
                y = yOther;
            }
            canvas.drawLine(startX, y, endX, y, mPaint);
            canvas.rotate(pDegree, mCenterX, mCenterY);
        }
        canvas.restore();

        //弧上画标签
        if (drawLabelInterval <= 0) return;//不画标签
        float labelRadius;//文字开始坐标所在圆弧的半径
        if (labelPosition == LABEL_POSITION_OUTER) {
            //标签画在圆外
            labelRadius = mRadius + labelMargin;
        } else {
            //标签画在圆内
            if (drawLabelIntervalLong) {
                labelRadius = mRadius - arcStrokeWidth - clockPointLength * 2 - labelMargin;
            } else {
                labelRadius = mRadius - arcStrokeWidth - clockPointLength - labelMargin;
            }
        }
        //每份的值
        float pValue = (maxValue - minValue) / (clockPointCount);
        float cutDegrees;
        float textX;
        float baseLine;
        mTextPaint.setColor(labelColor);
        mTextPaint.setTextSize(labelSize);
        mTextPaint.getFontMetrics(mFontMetrics);
        float textHeight = -(mFontMetrics.ascent + mFontMetrics.descent);
        if (DEBUG) {
            LogUtils.d(TAG, "drawDividerAndText:textHeight="+textHeight);
        }

        //画文字所在的圆
       /* mPaint.setStrokeWidth(3);
        mPaint.setColor(Color.YELLOW);
        canvas.drawCircle(mCenterX,mCenterY,labelRadius,mPaint);*/
        ValueFormat valueFormat = getValueFormat();
        for (int i = 0; i <= clockPointCount; i++) {
            if (i % drawLabelInterval == 0) {
                if(i==clockPointCount&&arcDegrees>=360){
                    break;
                }
                String valueText = valueFormat.getValueFormat(i * pValue+minValue);
                cutDegrees = i * pDegree + startDegree;
                textX = MathUtils.getArcXPoint(mCenterX, labelRadius, cutDegrees);
                baseLine = MathUtils.getArcYPoint(mCenterY, labelRadius, cutDegrees);
                float clockPointDegrees=cutDegrees%360;
              /*  if (DEBUG) {
                    LogUtils.d(TAG, "drawDividerAndText:clockPointDegrees"+i+":"+clockPointDegrees);
                }*/

                if(clockPointDegrees>90-D && clockPointDegrees<=90+D){
                    //底部
                    mTextPaint.setTextAlign(Paint.Align.CENTER);
                    if(labelPosition==LABEL_POSITION_OUTER){
                        baseLine+=textHeight;
                    }
                }else if(clockPointDegrees>90+D && clockPointDegrees<=270-D){
                    //左边
                    if(labelPosition==LABEL_POSITION_OUTER){
                        mTextPaint.setTextAlign(Paint.Align.RIGHT);
                    }else{
                        mTextPaint.setTextAlign(Paint.Align.LEFT);
                    }
                    baseLine+=textHeight/2;
                }else if(clockPointDegrees>270-D && clockPointDegrees<=270+D){
                    //顶部
                    mTextPaint.setTextAlign(Paint.Align.CENTER);
                    if(labelPosition==LABEL_POSITION_OUTER){

                    }else{
                        baseLine+=textHeight;
                    }
                }else{
                    //右边
                    if(labelPosition==LABEL_POSITION_OUTER){
                        mTextPaint.setTextAlign(Paint.Align.LEFT);
                    }else{
                        mTextPaint.setTextAlign(Paint.Align.RIGHT);
                    }
                    baseLine+=textHeight/2;
                }
                canvas.drawText(valueText, textX, baseLine, mTextPaint);
            }
        }

    }

    private float getPDegrees() {
        return (arcDegrees - arcOffsetDegrees * 2) * 1.0f / (clockPointCount);
    }



   //绘制刻度和数字:计算圆弧上点的坐标绘制发
   /* @Deprecated
    private void drawDividerAndText1(Canvas canvas) {
        float r1 = mRadius - arcStrokeWidth + 0;
        ;//刻度线起点坐标所在的圆弧半径
        float r2 = mRadius - arcStrokeWidth - clockPointLength;
        ;//刻度线终点坐标所在的圆弧半径
        float r2_ = mRadius - arcStrokeWidth - clockPointLength * 2;
        ;//长刻度线终点坐标所在的圆弧半径
        float r3;//文字开始坐标所在圆弧的半径
        if (labelPosition == LABEL_POSITION_OUTER) {
            //标签画在圆外
            r3 = mRadius + labelMargin;
        } else {
            //标签画在圆内
            if (drawLabelIntervalLong) {
                r3 = r2_ - labelMargin;
            } else {
                r3 = r2 - labelMargin;
            }
        }

        float startDegree = getStartDegree() + arcOffsetDegrees;
        float arcDegree;
        //每份的弧度
        float pDegree = (arcDegrees - arcOffsetDegrees * 2) * 1.0f / (clockPointCount);
        //每份的值
        float pValue = (maxValue - minValue) / (clockPointCount);

        mTextPaint.setColor(labelColor);
        mTextPaint.setTextSize(labelSize);
        mPaint.setColor(clockPointColor);
        mPaint.setStrokeWidth(clockPointWidth);
        int cPosition = (clockPointCount) / 2;//圆弧上中点的位子
        float startX;
        float startY;
        float endX;
        float endY;
        boolean drawValue = false;

        for (int i = 0; i < clockPointCount + 1; i++) {
            arcDegree = i * pDegree + startDegree;
            if (drawLabelInterval != 0) {
                drawValue = i % drawLabelInterval == 0;
            }
            startX = MathUtils.getArcXPoint(mCenterX, r1, arcDegree);
            startY = MathUtils.getArcYPoint(mCenterY, r1, arcDegree);
            if (drawValue && drawLabelIntervalLong) {
                endX = MathUtils.getArcXPoint(mCenterX, r2_, arcDegree);
                endY = MathUtils.getArcYPoint(mCenterY, r2_, arcDegree);
                mPaint.setStrokeWidth(clockPointWidth * 2);
            } else {
                endX = MathUtils.getArcXPoint(mCenterX, r2, arcDegree);
                endY = MathUtils.getArcYPoint(mCenterY, r2, arcDegree);
                mPaint.setStrokeWidth(clockPointWidth);
            }
            if (DEBUG) {
                LogUtils.d(TAG, "drawDividerAndText:startX=" + startX + ",startY=" + startY + ",endX=" + endX + ",endY=" + endY);
            }

            canvas.drawLine(startX, startY, endX, endY, mPaint);
            if (drawValue) {
                if (labelPosition == LABEL_POSITION_OUTER) {
                    //标签画在圆外
                    if (i < cPosition) {
                        //圆弧左边的文字右对齐
                        mTextPaint.setTextAlign(Paint.Align.RIGHT);
                    } else if (i == cPosition) {
                        //圆弧中间的文字居中对齐
                        mTextPaint.setTextAlign(Paint.Align.CENTER);
                    } else {
                        //圆弧右边的文字左对齐
                        mTextPaint.setTextAlign(Paint.Align.LEFT);
                    }
                } else {
                    //标签画在园内
                    if (i < cPosition) {
                        //圆弧左边的文字左对齐
                        mTextPaint.setTextAlign(Paint.Align.LEFT);
                    } else if (i == cPosition) {
                        //圆弧中间的文字居中对齐
                        mTextPaint.setTextAlign(Paint.Align.CENTER);
                    } else {
                        //圆弧右边的文字右对齐
                        mTextPaint.setTextAlign(Paint.Align.RIGHT);
                    }
                }
                startX = MathUtils.getArcXPoint(mCenterX, r3, arcDegree);
                startY = MathUtils.getArcYPoint(mCenterY, r3, arcDegree);

                String valueFormat = this.valueFormat.getValueFormat(i * pValue);
                canvas.drawText(valueFormat, startX, startY, mTextPaint);
            }

        }

    }*/


    /**
     * 画指针
     *
     * @param canvas
     */
    private void drawPointer(Canvas canvas) {
        //画中线
       /* mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(1);
        mPaint.setColor(0xff0000ff);
        canvas.drawLine(0, mCenterY, getWidth(), mCenterY, mPaint);
        canvas.drawLine(mCenterX, 0, mCenterX, getHeight(), mPaint);*/

        int intrinsicWidth = pointerDrawable.getIntrinsicWidth();
        int intrinsicHeight = pointerDrawable.getIntrinsicHeight();
        //指针缩放倍数
        float pointerDrawableScale = mRadius * pointerDrawableLengthRatio / intrinsicWidth;
        float drawableWidth = intrinsicWidth * pointerDrawableScale;
        float drawableHeight = intrinsicHeight * pointerDrawableScale;

        float banH = drawableHeight / 2;
        float offsetX = drawableWidth * pointerDrawableCenterRatio;
        int right = (int) (mCenterX + offsetX);
        int left = (int) (right - drawableWidth);
        int top = (int) (mCenterY - banH);
        int bottom = (int) (mCenterY + banH);

        pointerDrawable.setBounds(left, top, right, bottom);

        canvas.save();
        //偏移的角度
        float offDegree = (value-minValue) / (maxValue-minValue) * arcDegrees;
        float rotate = offDegree + ((180 - arcDegrees) >> 1);
        canvas.rotate(rotate, mCenterX, mCenterY);
        pointerDrawable.draw(canvas);
        canvas.restore();
    }




    public void setDegrees(int arcDegrees) {
        this.arcDegrees = arcDegrees;
        requestLayout();
    }

    public void setLabelPosition(int labelPosition) {
        this.labelPosition = labelPosition;
        requestLayout();
        //        postInvalidate();
    }

    public int getLabelPosition() {
        return labelPosition;
    }

    public void setMaxValue(float maxValue) {
        this.maxValue = maxValue;
    }

    public float getMaxValue() {
        return maxValue;
    }

    public void setMinValue(float minValue) {
        this.minValue = minValue;
    }

    public float getMinValue() {
        return minValue;
    }
    public void setValue(float value) {
        this.oldValue = this.value;
        this.value = value;
    }

    public float getValue() {
        return value;
    }



    public void startAnim(int during) {
        if(during<=0){
            during = ((int) (Math.abs(oldValue - value) / 360 * 5000));
            if (DEBUG) {
                LogUtils.d(TAG, "startAnim:value=" + value+",oldValue="+oldValue);
                LogUtils.d(TAG, "startAnim:during=" + during);
            }
            if (during < 200) {
                during = 200;
            }
        }
        ValueAnimator anim = ValueAnimator.ofFloat(oldValue, value);
        anim.setDuration(during);
        anim.setInterpolator(new LinearInterpolator());
        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                value = (float) animation.getAnimatedValue();
                if (DEBUG) {
                    LogUtils.d(TAG, "onAnimationUpdate:value=" + value);
                }
                postInvalidate();
            }
        });
        anim.start();
    }



    public ValueFormat getValueFormat() {
        if (valueFormat == null) {
            valueFormat = new DefaultValueFormat();
        }
        return valueFormat;
    }

    public void setValueFormat(ValueFormat valueFormat) {
        this.valueFormat = valueFormat;
    }

    public interface ValueFormat {
        String getValueFormat(float value);
    }


    public static class DefaultValueFormat implements ValueFormat {

        @Override
        public String getValueFormat(float value) {
//            return Float.toString(value);
            if(value==((int)(value))){
                return ((int)(value))+"";
            }else{
                return MathUtils.getDecimal(value,2)+"";
            }

        }
    }


}
